﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.ModelObject;
using CommonTagTime;
using CommonTagTime.Business;
using System.Web.Security;
using System.Xml;

public partial class admin_usercontrol_LeftAdminMenu : System.Web.UI.UserControl
{
    public string result = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
    }

    private void LoadData()
    {
        SitePage p = (SitePage)this.Page;
        busaspnet_Roles objBus = new busaspnet_Roles();
        aspnet_Roles objRole = objBus.GetMaxRole(Membership.ApplicationName, p.UserloginId);
        if (objRole != null)
        {
            string url = Server.MapPath("~/admin/usercontrol/" + objRole.MenuFile);
            GetAdminMenu(p, url);
        }
    }
    private void GetAdminMenu(SitePage p, string url)
    {
        try
        {
            result += "<ul id='main-nav'>";
            XmlDocument mDocument = new XmlDocument();
            mDocument.Load(url);
            XmlNode mCurrentNode = mDocument.DocumentElement;
            XmlNodeList nodeList = mCurrentNode.SelectNodes("//menus");
            foreach (XmlNode parent in nodeList)
            {
                result += CreatedMenu(p, parent);
            }
            result += "</ul>";
        }
        catch
        {
        }

    }
    private string CreatedMenu(SitePage p, XmlNode parent)
    {
        bool selectTag = false;
        string result = string.Empty;
        XmlDocument mDocument = new XmlDocument();
        mDocument.LoadXml("<child>" + parent.InnerXml + "</child>");
        string name = string.Empty;
        string link = string.Empty;
        string isActive = string.Empty;
        foreach (XmlNode node in mDocument.FirstChild)
        {
            name = node.InnerText;
            link = GetNodeValue(node, "link");
            isActive = GetNodeValue(node, "isActive");
            if (isActive == "1")
            {
               
                string pageQuery = p.GetPageQuery.Trim();
                if (pageQuery != string.Empty)
                {
                    if (link.IndexOf('?') > 0)
                    {
                        pageQuery = "?" + pageQuery.Trim();
                    }
                    else pageQuery = string.Empty;
                   
                }
                if (link.Trim().ToLower()==p.GetCurrentPageUrl.Trim().ToLower() + pageQuery.ToLower())
                {
                    selectTag = true;
                    result += "<li><a class='current' title='product' href='" + link + "'>" + name + "</a></li>";
                }
                else result += "<li><a title='product' href='" + link + "'>" + name + "</a></li>";
            }
        }
        if (!selectTag)
        {
            string expanLink = GetNodeValue(parent, "expandLink");
            if (expanLink.ToLower().Contains(p.GetCurrentPageUrl.Trim().ToLower()))
                selectTag = true;
        }
        if (selectTag)
            result = "<li><a href='#' class='nav-top-item current'>" + GetNodeValue(parent, "name") + "</a><ul style='display: block;'>" + result + "</ul></li>";
        else
            result = "<li><a href='#' class='nav-top-item'>" + GetNodeValue(parent, "name") + "</a><ul>" + result + "</ul></li>";
        return result;

    }
    private string GetNodeValue(XmlNode node, string attribute)
    {
        try
        {
            return node.Attributes[attribute].Value.ToString();
        }
        catch
        {
        }
        return string.Empty;
    }
}